package com.cos.hospital.dao;

import android.content.Context;

import com.cos.hospital.dao.beandao.DbInfo;
import com.cos.hospital.dao.renewal.DbManager;
import com.cos.hospital.model.bean.user.UserInfoBean;
import com.cos.hospital.tool.UTxt;

/**
 * @Description: 穴位数据库辅助
 * @Author: Kosmos
 * @Date: 2018年10月08日 15:45
 * @Email: KosmoSakura@foxmail.com
 */
public class HelperInfoDb {
    interface Type {
        int nameUser = 1;
        int nameHost = 2;
        int nameDept = 3;
        int birthday = 4;
        int avatar = 5;
        int telephone = 6;
        int qualification = 7;
        int wxaCode = 8;
    }

    /**
     * 增
     */
    public static void insert(Context context, DbInfo bean) {
        if (context == null) {
            return;
        }
        DbManager.getDaoSession(context).getDbInfoDao().insertOrReplace(bean);
    }

    /**
     * 删
     */
    public static void deleteById(Context context, long userId) {
        if (context == null) {
            return;
        }
        DbManager.getDaoSession(context).getDbInfoDao().deleteByKey(userId);
    }

    public static void deleteAll(Context context) {
        if (context == null) {
            return;
        }
        DbManager.getDaoSession(context).getDbInfoDao().deleteAll();
    }

    /**
     * 改
     */
    public static void update(Context context, DbInfo bean) {
        if (context == null) {
            return;
        }
        DbManager.getDaoSession(context).getDbInfoDao().update(bean);
    }

    public static void update(Context context, int userid, UserInfoBean bean) {
        if (context == null || bean == null) {
            return;
        }
        DbInfo info = HelperInfoDb.SearchById(context, userid);
        info.setBirthday(bean.getFldBirthday());
        info.setAge(bean.getFldAge());
        info.setWxaCode(bean.getFldWXACode());
        info.setNameUser(bean.getFldCustomerName());
        info.setNameDept(bean.getFldDeptName());
        info.setQualification(bean.getFldDoctorQualification());
        info.setAvatar(bean.getFldHeadPortrait());
        info.setNameHost(bean.getFldHosName());
        info.setCodeHospital(bean.getFldHospitalId());
        info.setCodeJob(bean.getFldJobTitle());
        info.setCodeSex(bean.getFldSex());
        DbManager.getDaoSession(context).getDbInfoDao().update(info);
    }

    /**
     * 查
     */
    public static DbInfo SearchById(Context context, long userid) {
        if (context == null) {
            return null;
        }
        return DbManager.getDaoSession(context).getDbInfoDao().load(userid);
    }

    /**
     * @param userid 用户id
     * @param flag   待检查的字段
     * @return 是否为空
     */
    public static boolean checkEmpty(Context context, long userid, int... flag) {
        if (context == null) {
            return true;
        }
        return checkEmpty(SearchById(context, userid), flag);
    }

    public static boolean checkEmpty(DbInfo info, int... flag) {
        if (info == null) {
            return true;
        } else {
            boolean empty = false;
            if (flag == null || flag.length == 0) {
                empty = UTxt.isEmpty(info.getNameUser()) || UTxt.isEmpty(info.getNameHost()) || UTxt.isEmpty(info.getNameDept()) ||
                    UTxt.isEmpty(info.getBirthday()) || UTxt.isEmpty(info.getAvatar()) || UTxt.isEmpty(info.getQualification()) ||
                    UTxt.isEmpty(info.getWxaCode());
            } else {
                int i = 0;
                while (i < flag.length) {
                    if (empty) {
                        break;
                    }
                    switch (flag[i]) {
                        case Type.nameUser:
                            empty = UTxt.isEmpty(info.getNameUser());
                            break;
                        case Type.nameHost:
                            empty = UTxt.isEmpty(info.getNameHost());
                            break;
                        case Type.nameDept:
                            empty = UTxt.isEmpty(info.getNameDept());
                            break;
                        case Type.birthday:
                            empty = UTxt.isEmpty(info.getBirthday());
                            break;
                        case Type.avatar:
                            empty = UTxt.isEmpty(info.getAvatar());
                            break;
                        case Type.telephone:
                            empty = UTxt.isEmpty(info.getTelephone());
                            break;
                        case Type.qualification:
                            empty = UTxt.isEmpty(info.getQualification());
                            break;
                        case Type.wxaCode:
                            empty = UTxt.isEmpty(info.getWxaCode());
                            break;
                    }
                    i++;
                }
            }
            return empty;
        }
    }
}
